package com.zenithmind.document.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

/**
 * 文档标签创建DTO
 * 遵循单一职责原则：专注于标签创建数据传输
 * 遵循开闭原则：通过验证注解支持扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档标签创建DTO")
public class DocumentTagCreateDTO {

    @Schema(description = "标签名称", required = true, example = "Java")
    @NotBlank(message = "标签名称不能为空")
    @Size(min = 1, max = 50, message = "标签名称长度必须在1-50字符之间")
    @Pattern(regexp = "^[\\u4e00-\\u9fa5a-zA-Z0-9_\\-\\.\\s]+$", message = "标签名称只能包含中文、英文、数字、下划线、连字符、点号和空格")
    private String name;

    @Schema(description = "标签描述", example = "Java编程语言相关文档")
    @Size(max = 500, message = "标签描述不能超过500字符")
    private String description;

    @Schema(description = "标签颜色", example = "#FF5722")
    @Pattern(regexp = "^#[0-9A-Fa-f]{6}$", message = "标签颜色必须是有效的十六进制颜色值")
    private String color;

    @Schema(description = "标签图标", example = "fa-java")
    @Size(max = 100, message = "标签图标不能超过100字符")
    private String icon;

    @Schema(description = "标签分类", example = "编程语言")
    @Size(max = 100, message = "标签分类不能超过100字符")
    private String category;

    @Schema(description = "父标签ID", example = "tag123456")
    private String parentTagId;

    @Schema(description = "标签级别", example = "1")
    private Integer level;

    @Schema(description = "排序权重", example = "100")
    private Integer sortWeight;

    @Schema(description = "是否系统标签", example = "false")
    private Boolean isSystemTag;

    @Schema(description = "是否公开标签", example = "true")
    private Boolean isPublic;

    @Schema(description = "是否推荐标签", example = "false")
    private Boolean isRecommended;

    @Schema(description = "是否热门标签", example = "false")
    private Boolean isHot;

    @Schema(description = "标签别名", example = "java,JAVA,Java语言")
    private String aliases;

    @Schema(description = "相关关键词", example = "编程,开发,后端")
    private String keywords;

    @Schema(description = "标签权重", example = "1.0")
    private Double weight;

    @Schema(description = "使用限制", example = "{\"maxUsage\": 1000, \"userLevel\": \"normal\"}")
    private String usageRestrictions;

    @Schema(description = "标签规则", example = "{\"autoApply\": true, \"conditions\": [\"title contains java\"]}")
    private String tagRules;

    @Schema(description = "创建人ID", example = "user123456")
    private String creatorId;

    @Schema(description = "创建来源", example = "MANUAL", allowableValues = {"MANUAL", "AUTO", "IMPORT", "SYSTEM"})
    private String createSource;

    @Schema(description = "语言代码", example = "zh-CN")
    private String languageCode;

    @Schema(description = "地域代码", example = "CN")
    private String regionCode;

    @Schema(description = "标签状态", example = "ACTIVE", allowableValues = {"ACTIVE", "INACTIVE", "PENDING", "DEPRECATED"})
    private String status;

    @Schema(description = "扩展属性", example = "{\"difficulty\": \"beginner\", \"popularity\": \"high\"}")
    private String extendedProperties;

    @Schema(description = "备注", example = "系统自动创建的编程语言标签")
    private String remarks;

    /**
     * 创建简单标签
     */
    public static DocumentTagCreateDTO createSimpleTag(String name, String creatorId) {
        DocumentTagCreateDTO dto = new DocumentTagCreateDTO();
        dto.setName(name);
        dto.setCreatorId(creatorId);
        dto.setCreateSource("MANUAL");
        dto.setIsSystemTag(false);
        dto.setIsPublic(true);
        dto.setIsRecommended(false);
        dto.setIsHot(false);
        dto.setLevel(1);
        dto.setSortWeight(100);
        dto.setWeight(1.0);
        dto.setStatus("ACTIVE");
        return dto;
    }

    /**
     * 创建分类标签
     */
    public static DocumentTagCreateDTO createCategoryTag(String name, String category, String description, String creatorId) {
        DocumentTagCreateDTO dto = createSimpleTag(name, creatorId);
        dto.setCategory(category);
        dto.setDescription(description);
        dto.setIsRecommended(true);
        dto.setSortWeight(200);
        return dto;
    }

    /**
     * 创建系统标签
     */
    public static DocumentTagCreateDTO createSystemTag(String name, String description, String category) {
        DocumentTagCreateDTO dto = new DocumentTagCreateDTO();
        dto.setName(name);
        dto.setDescription(description);
        dto.setCategory(category);
        dto.setCreateSource("SYSTEM");
        dto.setIsSystemTag(true);
        dto.setIsPublic(true);
        dto.setIsRecommended(true);
        dto.setLevel(1);
        dto.setSortWeight(500);
        dto.setWeight(1.5);
        dto.setStatus("ACTIVE");
        return dto;
    }

    /**
     * 创建子标签
     */
    public static DocumentTagCreateDTO createChildTag(String name, String parentTagId, String creatorId) {
        DocumentTagCreateDTO dto = createSimpleTag(name, creatorId);
        dto.setParentTagId(parentTagId);
        dto.setLevel(2);
        dto.setSortWeight(50);
        return dto;
    }

    /**
     * 创建热门标签
     */
    public static DocumentTagCreateDTO createHotTag(String name, String description, String creatorId) {
        DocumentTagCreateDTO dto = createSimpleTag(name, creatorId);
        dto.setDescription(description);
        dto.setIsHot(true);
        dto.setIsRecommended(true);
        dto.setSortWeight(300);
        dto.setWeight(1.2);
        return dto;
    }

    /**
     * 验证DTO数据
     */
    public boolean isValid() {
        if (name == null || name.trim().isEmpty()) return false;
        if (name.length() > 50) return false;
        
        // 验证标签名称格式
        if (!name.matches("^[\\u4e00-\\u9fa5a-zA-Z0-9_\\-\\.\\s]+$")) return false;
        
        // 验证描述长度
        if (description != null && description.length() > 500) return false;
        
        // 验证颜色格式
        if (color != null && !color.matches("^#[0-9A-Fa-f]{6}$")) return false;
        
        // 验证图标长度
        if (icon != null && icon.length() > 100) return false;
        
        // 验证分类长度
        if (category != null && category.length() > 100) return false;
        
        // 验证级别范围
        if (level != null && (level < 1 || level > 10)) return false;
        
        // 验证权重范围
        if (weight != null && (weight < 0.1 || weight > 10.0)) return false;
        
        return true;
    }

    /**
     * 是否为系统标签
     */
    public boolean isSystemTag() {
        return isSystemTag != null && isSystemTag;
    }

    /**
     * 是否为公开标签
     */
    public boolean isPublicTag() {
        return isPublic == null || isPublic;
    }

    /**
     * 是否为推荐标签
     */
    public boolean isRecommendedTag() {
        return isRecommended != null && isRecommended;
    }

    /**
     * 是否为热门标签
     */
    public boolean isHotTag() {
        return isHot != null && isHot;
    }

    /**
     * 是否为子标签
     */
    public boolean isChildTag() {
        return parentTagId != null && !parentTagId.trim().isEmpty();
    }

    /**
     * 是否有别名
     */
    public boolean hasAliases() {
        return aliases != null && !aliases.trim().isEmpty();
    }

    /**
     * 是否有关键词
     */
    public boolean hasKeywords() {
        return keywords != null && !keywords.trim().isEmpty();
    }

    /**
     * 是否有使用限制
     */
    public boolean hasUsageRestrictions() {
        return usageRestrictions != null && !usageRestrictions.trim().isEmpty();
    }

    /**
     * 是否有标签规则
     */
    public boolean hasTagRules() {
        return tagRules != null && !tagRules.trim().isEmpty();
    }

    /**
     * 获取别名列表
     */
    public String[] getAliasArray() {
        if (aliases == null || aliases.trim().isEmpty()) {
            return new String[0];
        }
        return aliases.split(",");
    }

    /**
     * 获取关键词列表
     */
    public String[] getKeywordArray() {
        if (keywords == null || keywords.trim().isEmpty()) {
            return new String[0];
        }
        return keywords.split(",");
    }

    /**
     * 获取标签全名（包含分类）
     */
    public String getFullName() {
        if (category != null && !category.trim().isEmpty()) {
            return category + "/" + name;
        }
        return name;
    }

    /**
     * 获取默认颜色
     */
    public String getDefaultColor() {
        if (color != null && !color.trim().isEmpty()) {
            return color;
        }
        
        // 根据分类返回默认颜色
        if (category != null) {
            switch (category.toLowerCase()) {
                case "编程语言": return "#FF5722";
                case "技术框架": return "#2196F3";
                case "工具软件": return "#4CAF50";
                case "文档类型": return "#FF9800";
                case "项目管理": return "#9C27B0";
                default: return "#607D8B";
            }
        }
        
        return "#607D8B";
    }

    /**
     * 获取默认图标
     */
    public String getDefaultIcon() {
        if (icon != null && !icon.trim().isEmpty()) {
            return icon;
        }
        
        // 根据分类返回默认图标
        if (category != null) {
            switch (category.toLowerCase()) {
                case "编程语言": return "fa-code";
                case "技术框架": return "fa-cogs";
                case "工具软件": return "fa-tools";
                case "文档类型": return "fa-file";
                case "项目管理": return "fa-project-diagram";
                default: return "fa-tag";
            }
        }
        
        return "fa-tag";
    }

    /**
     * 标签创建来源枚举
     */
    public enum CreateSource {
        MANUAL("手动创建"),
        AUTO("自动创建"),
        IMPORT("导入创建"),
        SYSTEM("系统创建");

        private final String description;

        CreateSource(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 标签状态枚举
     */
    public enum TagStatus {
        ACTIVE("活跃"),
        INACTIVE("非活跃"),
        PENDING("待审核"),
        DEPRECATED("已废弃");

        private final String description;

        TagStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
